import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, useForm } from "@/components/Form";
import { create_emergencies, update_emergencies } from "@/api/EmergencyDictionary";
import { useTranslation } from "react-i18next";
const UserRoleDrawer = (props: { register: Fn; reload: Fn; getTitle: string }) => {
	let utilA = "viewComponents.patientInformation";
	const { t } = useTranslation();
	const userRoleFormSchema = [
		// {
		// 	label: "编号",
		// 	field: "id",
		// 	show: false,
		// 	component: "Input"
		// },
		{
			label: t(`${utilA}.emgId`),
			field: "emgId",
			component: "Input",
			// show: false,
			colProps: { span: 24 },
			componentProps: {
				disabled: true
			}
		},
		{
			label: t("felix_ui.ReferralID"),
			field: "transId",
			component: "Input",
			colProps: { span: 24 }
		},
		{
			label: t("felix_ui.InsuranceID"),
			field: "insId",
			component: "Input",
			colProps: { span: 24 }
		},
		{
			label: t("felix_ui.CheckID"),
			field: "examId",
			component: "Input",
			colProps: { span: 24 }
		},
		{
			label: t("felix_ui.FamilyID"),
			field: "famId",
			component: "Input",
			colProps: { span: 24 }
		},
		{
			label: t("felix_ui.time_of_arrival"),
			field: "emgDtime",
			component: "DatePicker",
			colProps: { span: 24 }
		},
		{
			label: t("felix_ui.referralType"),
			field: "referralType",
			component: "Select",
			colProps: { span: 24 },
			componentProps: {
				options: [
					{ label: t("felix_ui.health_institutions"), value: "1" },
					{ label: t("felix_ui.not_mentioned"), value: "2" },
					{ label: t("felix_ui.orser"), value: "3" }
				]
			}
		},
		// {
		// 	label: "设施名称",
		// 	field: "facId",
		// 	component: "Input",
		// 	colProps: { span: 24 }
		// },
		{
			label: t("felix_ui.emgReason"),
			field: "emgReason",
			component: "Input",
			colProps: { span: 24 }
		},
		{
			label: t("felix_ui.deptId"),
			field: "deptId",
			component: "Select",
			colProps: { span: 24 },
			componentProps: {
				options: [
					{ label: t("felix_ui.emergency_room"), value: "1" },
					{ label: t("felix_ui.examination_room"), value: "2" },
					{ label: t("felix_ui.treatment_room"), value: "3" }
				]
			}
		},
		{
			label: t("felix_ui.status"),
			field: "status",
			component: "Select",
			componentProps: {
				options: [
					{ label: "开启", value: "0" },
					{ label: "关闭", value: "1" }
				]
			},
			colProps: { span: 24 }
		}
	];
	const [registerForm, { validateFields, resetFields, setFieldsValue }] = useForm({
		labelWidth: 120,
		schemas: userRoleFormSchema,
		showActionButtonGroup: false
	});

	const [registerDrawer, { closeDrawer }] = useDrawerInner(props.register, data => {
		console.log("打开Drawer", data);
		resetFields();
		setFieldsValue(data.record);
	});

	async function handleSubmit() {
		let values = await validateFields();
		values = { ...values, emgDtime: Date.parse(values.emgDtime) }; // 时分秒转成时间戳
		console.log("提交", values);
		if (props.getTitle == t("felix_ui.edit_emergency_info")) {
			await update_emergencies(values);
		} else {
			await create_emergencies(values);
		}
		props.reload();
		closeDrawer();
	}

	return (
		<BasicDrawer {...props} title={props.getTitle} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={registerForm} labelWrap={true} />
		</BasicDrawer>
	);
};

export default UserRoleDrawer;
